﻿using System.Collections.Generic;
using com.youzu.warh.protocol;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using UnityEngine.Rendering;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class NpcDetemineWeapon : AIBaseNode
    {
        // 调试武器id
        public BBParameter<int> debugWeaponId = -1;
        // 是否选取手雷
        public BBParameter<bool> isGrenade = false;
        // 返回值类型
        public BBParameter<ReturnValueType> returnValueType = ReturnValueType.ReturnSuccess;

        protected override void OnExecute()
        {
#if UNITY_EDITOR
            // 切换Debug武器
            if (debugWeaponId.value >= 0)
            {
                bool result = ChangeWeapon(debugWeaponId.value);
                EndActionByReturnValueType(result);
                return;
            }
#endif
            
            // 选择武器
            int selectedWeaponId = 0;
            if (isGrenade.value)
            {
                selectedWeaponId = _entity.WeaponSystem.GetCanUseFirstPriorityWeaponId(true);
            }
            if (selectedWeaponId == 0)
            {
                selectedWeaponId = _entity.WeaponSystem.GetCanUseFirstPriorityWeaponId(false);
            }
            
            // 切换武器
            {
                bool result = ChangeWeapon(selectedWeaponId);
                EndActionByReturnValueType(result);
            }
        }

        void EndActionByReturnValueType(bool result)
        {
            switch (returnValueType.value)
            {
                case ReturnValueType.ReturnSuccess:
                    {
                        EndAction(true);
                    }
                    break;
                case ReturnValueType.ReturnFailure:
                    {
                        EndAction(false);
                    }
                    break;
                case ReturnValueType.ReturnResult:
                    {
                        EndAction(result);
                    }
                    break;

                default:
                    {
                        EndAction(true);
                    }
                    break;
            }
        }

        bool ChangeWeapon(int selectedWeaponId)
        {
            // 切换武器
            _entity.WeaponSystem.EquipWeapon(selectedWeaponId);
            return _entity.battleAgent.ChangeWeapon(selectedWeaponId);
        }
        
    }
}